package com.climb.spingcloud.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConditionalOnClass(OpenAPI.class)
public class SwaggerAutoConfig {
    @Bean
    public OpenAPI ShopOpenAPI() {
        // 名字和创建的SecuritySchemes一致
        SecurityRequirement sr = new SecurityRequirement().addList("bearerAuth");
        List<SecurityRequirement> list = new ArrayList<>();
        list.add(sr);

        return new OpenAPI()
                .info(new Info().title("Shop API")
                        .description("网上商城")
                        .version("v0.0.1")
                        .license(new License().name("Apache 2.0").url("http://climbcloud.com")))
                .externalDocs(new ExternalDocumentation()
                        .description("Shop Wiki Documentation")
                        .url("https://climbcloud.com/docs"))
                // 这是基于HTTP协议的bearer认证实现
                .components(new Components().addSecuritySchemes("bearerAuth",
                        new SecurityScheme().type(SecurityScheme.Type.HTTP)
                                .scheme("bearer").bearerFormat("JWT")))
                // 应用安全需要
                .security(list);
    }
}